package com.itheima.health.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.User;
import com.itheima.health.service.User1Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：
 * @date ：Created in 2019/7/15
 * @description ：
 * @version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/user1")
public class UserController1 {
    @Reference
    private User1Service user1Service;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @RequestMapping("/findPage")
    @ResponseBody
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            return user1Service.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());

        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(0L, new ArrayList());
        }
    }
    /**
     * 添加用户
     */
    @RequestMapping("/add")
    public Result add(@RequestBody User user, Integer[] roleIds) {

        try {
            user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
            user1Service.add(user, roleIds);
            return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ADD_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            User byId = user1Service.findById(id);
            return new Result(true, MessageConst.ACTION_SUCCESS, byId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

    @RequestMapping("/findroleIdsByuser1Id")
    public Result findroleIdsByuser1Id(Integer id) {
        try {
            List<Integer> list = user1Service.findroleIdsByuser1Id(id);
            return new Result(true, MessageConst.ACTION_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConst.ACTION_FAIL);
        }
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user, Integer[] roleIds) {

        try {
            user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
            user1Service.edit(user, roleIds);
            return new Result(true, MessageConst.ACTION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            user1Service.deleteById(id);
            return  new Result(true,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }







}
